using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum PackageMode
{
    NORMAL,
    DELETE
}

public class PackagePanel : BasePanel
{
    private Transform UIMenu;
    private Transform UIMenuWeapon;
    private Transform UIMenuFood;
    private Transform UITabName;
    private Transform UICloseBtn;
    private Transform UICenter;
    private Transform UIScrollView;
    private Transform UIDetailPanel;
    private Transform UILeftBtn;
    private Transform UIRightBtn;
    private Transform UIWeaponNumber;
    private Transform UIDeletePanel;
    private Transform UIDeleteBackBtn;
    private Transform UIDeleteInfoText;
    private Transform UIDeleteConfirmBtn;
    private Transform UIBottomMenus;
    private Transform UIDeleteBtn;
    private Transform UIDetailBtn;

    public GameObject packageUIItem;
    public Transform scrollContent;
    public PackageMode keyMode = PackageMode.NORMAL;
    private string selectUId = "1";

    public string SelectUId
    {
        get { return selectUId; }
        set
        {
            selectUId = value;
            Debug.Log("刷新ui");
            RefreshDetailPanel();
        }
    }

    private string checkUId;

    public string CheckUId
    {
        get { return checkUId; }
        set
        {
            checkUId = value;
            //RefreshDetail();
        }
    }

    public List<string> uids = new List<string>();

    public void AddCheckUId(string uid)
    {
        if (uids.Count >= 100) return;
        if (this.uids.Contains(uid))
        {
            uids.Remove(uid);
        }
        else
        {
            uids.Add(uid);
        }

        RefreshDeletePanel();
    }

    private void RefreshDeletePanel()
    {
        RectTransform rectTransform = UIScrollView.GetComponent<ScrollRect>().content;
        foreach (Transform item in rectTransform)
        {
            PackageCell packageCell = item.GetComponent<PackageCell>();
            packageCell.RefreshDeleteState();
        }

        UIDeleteInfoText.GetComponent<Text>().text = string.Format("已选择{0}/100", uids.Count);
    }

    

    public void RefreshDetailPanel()
    {
        PackageLocalItem localItem = GameManager.Instance.GetPackageLocalItemByUid(selectUId);
        UIDetailPanel.GetComponent<DetailPanel>().RefreshUI(localItem);
    }

    protected override void Awake()
    {
        base.Awake();
        InitUI();
    }

    private void Start()
    {
        RefreshUI();
    }

    private void RefreshUI()
    {
        ResfreshScroll();
    }

    private void ResfreshScroll()
    {
        //清除子节点
        RectTransform rectTransform = UIScrollView.GetComponent<ScrollRect>().content;
        int childCount = rectTransform.transform.childCount;
        for (int i = 0; i < childCount; i++)
        {
            Transform child = rectTransform.transform.GetChild(i);
            Destroy(child.gameObject);
        }

        List<PackageLocalItem> sortPackageLocalItem = GameManager.Instance.SortPackageLocalItem();
        foreach (var packageLocalItem in sortPackageLocalItem)
        {
            GameObject packageItem = Instantiate(packageUIItem, scrollContent);
            PackageCell packageCell = packageItem.GetComponent<PackageCell>();
            packageCell.RefreshUI(packageLocalItem, this);
        }

        UIWeaponNumber.GetComponent<Text>().text =
            string.Format("武器 {0}/1000", PackageLocalData.Instance.tableItems.Count.ToString());
    }

    private void InitUI()
    {
        InitUIName();
        InitClick();
    }

    private void InitClick()
    {
        UIMenuWeapon.GetComponent<Button>().onClick.AddListener(OnClickWeapon);
        UIMenuFood.GetComponent<Button>().onClick.AddListener(OnClickFood);
        UICloseBtn.GetComponent<Button>().onClick.AddListener(OnClickClose);
        UILeftBtn.GetComponent<Button>().onClick.AddListener(OnClickLeft);
        UIRightBtn.GetComponent<Button>().onClick.AddListener(OnClickRight);

        UIDeleteBackBtn.GetComponent<Button>().onClick.AddListener(OnDeleteBack);
        UIDeleteConfirmBtn.GetComponent<Button>().onClick.AddListener(OnDeleteConfirm);
        UIDeleteBtn.GetComponent<Button>().onClick.AddListener(OnDelete);
        UIDetailBtn.GetComponent<Button>().onClick.AddListener(OnDetail);
    }

    private void OnDetail()
    {
        throw new System.NotImplementedException();
    }

    private void OnDelete()
    {
        keyMode = PackageMode.DELETE;
        UIDeletePanel.gameObject.SetActive(true);
    }

    private void OnDeleteConfirm()
    {
        if (uids.Count == 0)
        {
            return;
        }

        GameManager.Instance.DeleteLocalItem(uids);
        RefreshUI();
    }

    private void OnDeleteBack()
    {
        keyMode = PackageMode.NORMAL;
        UIDeletePanel.gameObject.SetActive(false);

        //重置删除列表
        uids.Clear();
        RefreshDeletePanel();
    }

    private void OnClickRight()
    {
        throw new System.NotImplementedException();
    }

    private void OnClickLeft()
    {
        throw new System.NotImplementedException();
    }

    private void OnClickClose()
    {
        ClosePanel();
    }

    private void OnClickFood()
    {
        throw new System.NotImplementedException();
    }

    private void OnClickWeapon()
    {
        throw new System.NotImplementedException();
    }

    private void InitUIName()
    {
        UIMenu = transform.Find("MiddleTop/Menu");
        UIMenuWeapon = transform.Find("MiddleTop/Menu/Weapon");
        UIMenuFood = transform.Find("MiddleTop/Menu/Food");
        UITabName = transform.Find("LeftTop/IconText");
        UICloseBtn = transform.Find("RightTop/Close");
        UIWeaponNumber = transform.Find("RightTop/IconText");
        UICenter = transform.Find("Center");
        UIScrollView = transform.Find("Center/Scroll View");
        UIDetailPanel = transform.Find("Center/DetailPanel");
        UILeftBtn = transform.Find("CenterLeft/Icon");
        UIRightBtn = transform.Find("CenterRight/Icon");

        UIDeletePanel = transform.Find("Bottom/DeletePanel");
        UIDeleteBackBtn = transform.Find("Bottom/DeletePanel/ReturnBtn");
        UIDeleteInfoText = transform.Find("Bottom/DeletePanel/InfoText");
        UIDeleteConfirmBtn = transform.Find("Bottom/DeletePanel/ConfirmBtn");
        UIBottomMenus = transform.Find("Bottom/BottomMenus");
        UIDeleteBtn = transform.Find("Bottom/BottomMenus/DeleteBtn");
        UIDetailBtn = transform.Find("Bottom/BottomMenus/DetailBtn");

        UIDeletePanel.gameObject.SetActive(false);
        UIBottomMenus.gameObject.SetActive(true);
    }
}